<?
class photoController extends iMVC_Controller{


    public function index(){

        $tm=new infoModel();
        $table=new iMVC_View_Table();
        //添加时列名不能为数字，为数字时array_merge会破坏键值
        $table->addCol('action',new iMVC_View_Table_Col_Action('修改'),10);
        $table->setCol('info_id',new iMVC_View_Table_Col_CheckBox(),11);
        $table->generateTable($tm->fetchAll());
        $this->assign('infoTable',$table);
        $this->display("info.tpl");

    }




    public function upPic(){
            $callback=$_GET['callback'];
            $type = array('gif', 'jpg', 'png', 'zip', 'rar');
            $upload = new iMVC_Image_Upload($_FILES['file'],'upload/pics', 5000000, $type);
            $num = $upload->upload();
            $i=0;
            if ($num != 0) {
                $str.="{";
                $str.="isSuc:true,pics:{";
                foreach( $upload->getSaveInfo() as $value){;
                    if($i!=0){
                        $str.=',';
                    }
                    $str.= $i.':"'.$this->getBaseUrl().$value['path'].'"'; 
                    $t=new iMVC_Image_Handler();
                    $file=$value['path'];
                    $img=getimagesize($file);
                    $width=$img[0];
                    $height=$img[1];
                    $k=$height/$width;
                    if($k>1){
                        $w=300; 
                        $h=$w*$k;
                    
                    }
                    else{
                        $h=300;
                        $w=$h/$k;
                    }
                    $t->setSrcImg($file);
                    $t->setCutType(0);//指明为手工裁切
                    $t->setSrcCutPosition(0,0);// 源图起点坐标
                    $t->setRectangleCut($width,$height);// 裁切尺寸
                    $t->setDstImg($file);
                    $t->createImg($w,$h); 
                    $i++;

                }
                $str.="}}";
                //todo 修改为返回json对象
                echo "<script>parent.$callback('$str');</script>";
            }
            else {

                $str.="{isSuc:false}";
                echo "<script>parent.$callback('$str');</script>";
            }

    }
    public function cutPic(){
        $file=$_GET['url'];
        $tmp=$file;
        $file=$_SERVER['DOCUMENT_ROOT'].$file;
        $x=$_GET['x'];
        $y=$_GET['y'];
        $h=$_GET['h'];
        $w=$_GET['w'];
        $t=new iMVC_Image_Handler();
        $t->setSrcImg($file);
        $t->setCutType(2);//指明为手工裁切
        $t->setSrcCutPosition($x,$y);// 源图起点坐标
        $t->setRectangleCut($w, $h);// 裁切尺寸
        $t->setDstImg($file);
        $t->createImg($w,$h); 
        echo $tmp;
    }
    
  public function upPhoto(){
            $id=$_POST['id'];
            $callback=$_GET['callback'];
            $type = array('gif', 'jpg', 'png', 'zip', 'rar');
            $upload = new iMVC_Image_Upload($_FILES['file'],'upload/pics', 5000000, $type);
            $num = $upload->upload();
            $i=0;
            if ($num != 0) {
                $str.="{";
                $str.="isSuc:true,pics:{";
                foreach( $upload->getSaveInfo() as $value){;
                    if($i!=0){
                        $str.=',';
                    }
                    $str.= $i.':"'.$this->getBaseUrl().$value['path'].'"'; 
                    $t=new iMVC_Image_Handler();
                    $file=$value['path'];
                    $img=getimagesize($file);
                    $width=$img[0];
                    $height=$img[1];
                    $k=$height/$width;
                    if($k>1){
                        $w=300; 
                        $h=$w*$k;
                    
                    }
                    else{
                        $h=300;
                        $w=$h/$k;
                    }
                    $t->setSrcImg($file);
                    $t->setCutType(0);//指明为手工裁切
                    $t->setSrcCutPosition(0,0);// 源图起点坐标
                    $t->setRectangleCut($width,$height);// 裁切尺寸
                    $t->setDstImg($file);
                    $t->createImg($w,$h); 

                    //将photo存入数据库
                    $photo=new photoModel();
                    $photo->photo_url=$file;
                    $photo->info_id=$id;
                    var_dump($photo);
                    $photo->save();
                    $i++;



                }
                $str.="}}";
                //todo 修改为返回json对象
                echo "<script>parent.$callback('$str');</script>";
            }
            else {

                $str.="{isSuc:false}";
                echo "<script>parent.$callback('$str');</script>";
            }

    }






}


// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
